<?php

namespace App\Http\Requests;
use Illuminate\Validation\Rule;

class ConfirmOrderRequest extends Request
{
    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'confirm_type'  => 'required|integer',                      // 确认订单的类型 1 购物车结算  2 立即购买
            'product_id'      => 'required_if:confirm_type,2|integer',    // 商品ID
            'sku_id'        => [
                'required_if:confirm_type,2',
                'integer',
                Rule::exists('product_skus', 'id')->where('product_id', $this->product_id),  // 规格ID且是属于这个商品的
            ],
            'amount'        => 'required_if:confirm_type,2|integer|max:999',          // 商品数量
            'cart_id'       => [
                'required_if:confirm_type,1'
            ],
        ];
    }

    public function messages()
    {
        return [
            'cart_id.required_if'       => '购物车ID不能为空',
            'cart_id.exists'            => '购物车ID不存在',
            'amount.max'                => '一次性购买数量不能大于999',
            'sku_id.exists'             => '商品规格不存在',
        ];
    }
}
